package com.symaster.backups.frame;

import com.symaster.backups.frame.element.card.CardPanel;
import com.symaster.backups.frame.element.menu.TopMenu;
import com.symaster.backups.frame.page.configs.Config;
import com.symaster.backups.frame.page.viewfiles.FileBackupView;
import com.symaster.backups.manager.EventManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import static com.symaster.backups.util.ComponentUtil.separatorPanel;

/**
 * @author yinmiao
 * @version 2024/2/5
 */
@Component
public class MainFrame extends JFrame implements TopMenu.ChangeListener {
    final JPanel contentPane;

    private final TopMenu topMenu;
    private final CardPanel cardPanel;
    private boolean init = false;
    private static final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1024));

    private void addItem(Item item) {
        topMenu.addItem(new TopMenu.Item(item.getClass().getSimpleName(), item.label()));
        cardPanel.add(item);
    }

    @Autowired
    public MainFrame(FileBackupView fileBackupView,
                     Config config,
                     EventManager eventManager) {
        super("文件自动备份系统");
        eventManager.setMainFrame(this);
        setContentPane(contentPane = new JPanel());
        contentPane.setLayout(new BorderLayout());
        contentPane.add(topMenu = getTopMenu(), BorderLayout.NORTH);

        JPanel jPanel = new JPanel();
        jPanel.setLayout(new BorderLayout());
        jPanel.add(separatorPanel(), BorderLayout.NORTH);
        jPanel.add(cardPanel = getCardPanel(), BorderLayout.CENTER);

        contentPane.add(jPanel, BorderLayout.CENTER);
        addItem(fileBackupView);
        addItem(config);
    }

    private static CardPanel getCardPanel() {
        return new CardPanel();
    }

    private TopMenu getTopMenu() {
        TopMenu topMenu = new TopMenu();
        topMenu.addChangeListener(this);
        return topMenu;
    }

    public void open() {
        Rectangle rectangle = new Rectangle(50, 50, 900, 600);
        open(rectangle);
    }

    public void open(Rectangle rectangle) {
        pack();
        setBounds(rectangle);
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        if (!init && cardPanel.getComponentCount() > 1) {
            init = true;
            Item component = (Item) cardPanel.getComponent(0);
            SwingUtilities.invokeLater(component::viewEvent);
        }

        super.paint(g);
    }

    @Override
    public void menuChange(String key, int index) {
        cardPanel.setViewIndex(index);
        java.awt.Component component = cardPanel.getComponent(index);
        if (component instanceof Item item) {
            threadPoolExecutor.execute(item::viewEvent);
        }
    }

    public static abstract class Item extends JComponent {
        public abstract String label();

        public void viewEvent() {

        }
    }
}
